home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gas / gassrc04.zoo / input-file.c < prev    next >
C/C++ Source or Header  |  1991-07-28  |  8KB  |  341 lines

  1. /* input_file.c - Deal with Input Files -
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * Confines all details of reading source bytes to this module.
  22.  * All O/S specific crocks should live here.
  23.  * What we lose in "efficiency" we gain in modularity.
  24.  * Note we don't need to #include the "as.h" file. No common coupling!
  25.  */
  26.  
  27. #define NDEBUG        /* JF remove asserts */
  28.  
  29. #ifdef USG
  30. #define index strchr
  31. /* JF:  What's the difference between _IOLBF and _IOFBF ? */
  32. #define setbuffer(stream, buf, size) setvbuf((stream), (buf), _IOFBF, (size))
  33. #endif
  34.  
  35. #include <stdio.h>
  36. #include <assert.h>
  37. /* #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <sys/file.h>
  40. #include <sys/wait.h> */
  41.  
  42. /* #include "style.h" */
  43. #include "input-file.h"
  44.  
  45. /* This variable is non-zero if the file currently being read should be
  46.    preprocessed by app.  It is zero if the file can be read straight in.
  47.  */
  48. int preprocess = 0;
  49.  
  50. void    as_perror();
  51.  
  52. /*
  53.  * This code opens a file, then delivers BUFFER_SIZE character
  54.  * chunks of the file on demand.
  55.  * BUFFER_SIZE is supposed to be a number chosen for speed.
  56.  * The caller only asks once what BUFFER_SIZE is, and asks before
  57.  * the nature of the input files (if any) is known.
  58.  */
  59.  
  60. #define BUFFER_SIZE (32 * 1024)
  61.  
  62. #ifdef atarist
  63. /* a little ditty here to do fread, filtering out '\r'. */
  64.  
  65. int filtering_fread(where, how_big, how_many, f)
  66. char * where;
  67. int how_big;
  68. int how_many;
  69. FILE * f;
  70. {
  71.   int i, j, c;
  72.  
  73.   for (i = 0 ; i < how_many ; )
  74.     {
  75.     for (j = 0 ; j < how_big ; )
  76.         {
  77.         c = fgetc(f);
  78.         if (c == EOF)
  79.             goto done;
  80.         if (c != '\r')
  81.             {
  82.             *where++ = c;
  83.             j++;
  84.             }
  85.         }
  86.     i++;
  87.     }
  88. done:
  89.   return(i);
  90. }
  91.  
  92. #else
  93.  
  94. static char in_buf[BUFFER_SIZE];
  95.   
  96. #endif
  97.  
  98.  
  99. /*
  100.  * We use static data: the data area is not sharable.
  101.  */
  102.  
  103. FILE *f_in;    /* JF do things the RIGHT way */
  104. /* static JF remove static so app.c can use file_name */
  105. char *    file_name;
  106.  
  107. /* These hooks accomodate most operating systems. */
  108.  
  109. void
  110. input_file_begin ()
  111. {
  112.   /* file_handle = -1; */
  113.   f_in = (FILE *)0;
  114. }
  115.  
  116. void
  117. input_file_end ()
  118. {
  119. }
  120.  
  121. int                /* Return BUFFER_SIZE. */
  122. input_file_buffer_size ()
  123. {
  124.   return (BUFFER_SIZE);
  125. }
  126.  
  127. int
  128. input_file_is_open ()
  129. {
  130.   /* return (file_handle >= 0); */
  131.   return f_in!=(FILE *)0;
  132. }
  133.  
  134. #ifdef DONTDEF        /* JF save old version in case we need it */
  135. void
  136. input_file_open (filename, preprocess, debugging)
  137.      char *    filename;    /* "" means use stdin. Must not be 0. */
  138.      int    preprocess;    /* TRUE if needs app. */
  139.      int    debugging;    /* TRUE if we are debugging assembler. */
  140. {
  141.   assert( filename != 0 );    /* Filename may not be NULL. */
  142.   if (filename [0])
  143.     {                /* We have a file name. Suck it and see. */
  144.       file_handle = open (filename, O_RDONLY, 0);
  145.       file_name = filename;
  146.     }
  147.   else
  148.     {                /* use stdin for the input file. */
  149.       file_handle = fileno (stdin);
  150.       file_name = "{standard input}"; /* For error messages. */
  151.     }
  152.   if (file_handle < 0)
  153.       as_perror ("Can't open %s for reading", file_name);
  154.   if ( preprocess )
  155.     {
  156. /*
  157.  * This code was written in haste for a frobbed BSD 4.2.
  158.  * I have a flight to catch: will someone please do proper
  159.  * error checks? - Dean.
  160.  */
  161.       int    pid;
  162.       char temporary_file_name [12];
  163.       int    fd;
  164.       union wait    status;
  165.       char    *mktemp();
  166.  
  167.       (void)strcpy (temporary_file_name, "#appXXXXXX");
  168.       (void)mktemp (temporary_file_name);
  169.       pid = vfork ();
  170.       if (pid == -1)
  171.     {
  172.       as_perror ("Vfork failed", file_name);
  173.       _exit (144);
  174.     }
  175.       if (pid == 0)
  176.     {
  177.       (void)dup2 (file_handle, fileno(stdin));
  178.       fd = open (temporary_file_name, O_WRONLY + O_TRUNC + O_CREAT, 0666);
  179.       if (fd == -1)
  180.         {
  181.           (void)write(2,"Can't open temporary\n",21);
  182.           _exit (99);
  183.         }
  184.       (void)dup2 (fd, fileno(stdout));
  185. /* JF for testing #define PREPROCESSOR "/lib/app" */
  186. #define PREPROCESSOR "./app"
  187.       execl (PREPROCESSOR, PREPROCESSOR, 0);
  188.       execl ("app","app",0);
  189.       (void)write(2,"Exec of app failed.  Get help.\n",31);
  190.       (void)unlink(temporary_file_name);
  191.       _exit (11);
  192.     }
  193.       (void)wait (& status);
  194.       if (status.w_status & 0xFF00)        /* JF was 0xF000, was wrong */
  195.     {
  196.       file_handle = -1;
  197.       as_warn( "Can't preprocess file \"%s\", status = %xx", file_name, status.w_status );
  198.     }
  199.       else
  200.     {
  201.       file_handle = open (temporary_file_name, O_RDONLY, 0);
  202.       if ( ! debugging && unlink(temporary_file_name))
  203.         as_perror ("Can't delete temp file %s", temporary_file_name);
  204.     }
  205.       if (file_handle == -1)
  206.       as_perror ("Can't retrieve temp file %s", temporary_file_name);
  207.     }
  208. }
  209. #else
  210.  
  211. void
  212. input_file_open (filename,pre)
  213.      char *    filename;    /* "" means use stdin. Must not be 0. */
  214.      int pre;
  215. {
  216.     int    c;
  217.     char    buf[80];
  218.  
  219.     preprocess = pre;
  220.  
  221.     assert( filename != 0 );    /* Filename may not be NULL. */
  222.     if (filename [0]) {    /* We have a file name. Suck it and see. */
  223.         f_in=fopen(filename,"r");
  224.         file_name=filename;
  225.     } else {            /* use stdin for the input file. */
  226.         f_in = stdin;
  227.         file_name = "{standard input}"; /* For error messages. */
  228.     }
  229.     if (f_in==(FILE *)0) {
  230.         as_perror ("Can't open %s for reading", file_name);
  231.         return;
  232.     }
  233. #if (!(defined(VMS) || defined(atarist) || defined(atariminix) || defined(CROSSHPUX)))
  234.     setbuffer(f_in,in_buf,BUFFER_SIZE);
  235. #endif /* VMS */
  236.     c=getc(f_in);
  237. #ifdef atarist
  238.     if(c == '\r') c = getc(f_in);
  239. #endif
  240.     if(c=='#') {    /* Begins with comment, may not want to preprocess */
  241.         c=getc(f_in);
  242. #ifdef atarist
  243.         if(c == '\r') c = getc(f_in);
  244. #endif
  245.         if(c=='N') {
  246.             fgets(buf,80,f_in);
  247.             if(!strcmp(buf,"O_APP\n"))
  248.                 preprocess=0;
  249.             if(!index(buf,'\n'))
  250.                 ungetc('#',f_in);    /* It was longer */
  251.             else
  252.                 ungetc('\n',f_in);
  253.         } else if(c=='\n')
  254.             ungetc('\n',f_in);
  255.         else
  256.             ungetc('#',f_in);
  257.     } else
  258.         ungetc(c,f_in);
  259.  
  260. #ifdef DONTDEF
  261.     if ( preprocess ) {
  262.         char temporary_file_name [17];
  263.         char    *mktemp();
  264.         FILE    *f_out;
  265.  
  266.         (void)strcpy (temporary_file_name, "/tmp/#appXXXXXX");
  267.         (void)mktemp (temporary_file_name);
  268.         f_out=fopen(temporary_file_name,"w+");
  269.         if(f_out==(FILE *)0)
  270.             as_perror("Can't open temp file %s",temporary_file_name);
  271.  
  272.             /* JF this will have to be moved on any system that
  273.                does not support removal of open files.  */
  274.         (void)unlink(temporary_file_name);/* JF do it NOW */
  275.         do_scrub(f_in,f_out);
  276.         (void)fclose(f_in);    /* All done with it */
  277.         (void)rewind(f_out);
  278.         f_in=f_out;
  279.     }
  280. #endif
  281. }
  282. #endif
  283.  
  284. char *
  285. input_file_give_next_buffer (where)
  286.      char *        where;    /* Where to place 1st character of new buffer. */
  287. {
  288.   char *    return_value;    /* -> Last char of what we read, + 1. */
  289.   register int    size;
  290.  
  291.   if (f_in == (FILE *)0)
  292.       return 0;
  293.       /*
  294.        * fflush (stdin); could be done here if you want to synchronise
  295.        * stdin and stdout, for the case where our input file is stdin.
  296.        * Since the assembler shouldn't do any output to stdout, we
  297.        * don't bother to synch output and input.
  298.        */
  299.   /* size = read (file_handle, where, BUFFER_SIZE); */
  300.   if(preprocess) {
  301.     char *p;
  302.     int n;
  303.     int ch;
  304.     extern FILE *scrub_file;
  305.     int scrub_from_file();
  306.     void scrub_to_file();
  307.     int do_scrub_next_char();
  308.  
  309.     scrub_file=f_in;
  310.     for(p=where,n=BUFFER_SIZE;n;--n) {
  311.         ch=do_scrub_next_char(scrub_from_file,scrub_to_file);
  312.         if(ch==EOF)
  313.             break;
  314.         *p++=ch;
  315.     }
  316.     size=BUFFER_SIZE-n;
  317.   } else
  318. #ifdef atarist
  319.   size= filtering_fread(where,sizeof(char),BUFFER_SIZE,f_in);
  320. #else
  321.   size= fread(where,sizeof(char),BUFFER_SIZE,f_in);
  322. #endif
  323.   if (size < 0)
  324.     {
  325.       as_perror ("Can't read from %s", file_name);
  326.       size = 0;
  327.     }
  328.   if (size)
  329.     return_value = where + size;
  330.   else
  331.     {
  332.       if (